package edu.sjsu.cs.files;

import java.util.InputMismatchException;
import java.util.Scanner;

/**
   COPYRIGHT(C) 2008. Natasha Khuri. All Rights Reserved.
   An ATM class that interacts with the user and deposits, withdraws,
   and checks BankAccount balance
   @author Natasha Khuri
   @version 1.01. 2008-12-08
*/
public class ATM
{

   public static void main(String[] args)
   {
      String message = "================================\n" +
                       "Enter the transaction type:\n" +
                       "\"d\" to deposit\n" +
                       "\"w\" to withdraw\n" +
                       "\"b\" to check current balance\n" +
                       "or \"q\" to quit\n" +
                       "================================\n";

      BankAccount account = new BankAccount(1000.0);

      System.out.println("Welcome to MYBANK ATM");
      System.out.println(message);
      Scanner keyboard = new Scanner(System.in);
      String input = keyboard.next();

      boolean notDone = true;
      while(notDone)
      {
         if (input.equalsIgnoreCase("q"))
         {
            notDone = false;
            System.out.println("Good-bye");
         }
         else
         {
            try
            {
               if(input.equalsIgnoreCase("d"))
                  depositMoney(account);
               else if(input.equalsIgnoreCase("w"))
                  withdrawMoney(account);
               else if(input.equalsIgnoreCase("b"))
                  printBalance(account);
               else
                  System.out.println("Please enter a valid transaction");
            }
            catch(IllegalArgumentException e)
            {
               System.out.println(e.getMessage());
            }
            catch(InputMismatchException e)
            {
               System.out.println("Incorrect input.");
            }
            System.out.println(message);
            input = keyboard.next();
         }
      }
   }

   /**
    * Method to print a balance of userAccount
    * @param userAccount BankAccount object
    */
   private static void printBalance(BankAccount userAccount)
   {
      System.out.println("Your current balance is " + userAccount.getBalance());

   }

   /**
    * Method to withdraw amount specified by the user from userAccount
    * @param userAccount BankAccount object
    */
   private static void withdrawMoney(BankAccount userAccount) throws IllegalArgumentException

   {
      Scanner keyboard = new Scanner(System.in);
      System.out.println("Enter amount to withdraw");
      double amount = keyboard.nextDouble();
      userAccount.withdraw(amount);
      
   }

   /**
    * Method to deposit amount specified by the user to userAccount
    * @param userAccount
    */
   private static void depositMoney(BankAccount userAccount) throws IllegalArgumentException
   {
      Scanner keyboard = new Scanner(System.in);
      System.out.println("Enter amount to deposit");
      double amount = keyboard.nextDouble();
      userAccount.deposit(amount);
      
   }
}
